package com.chang.controller;

import com.chang.entity.Essay;
import com.chang.service.EssayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Date;

@Controller
public class EssayController {

    @Autowired
    private EssayService essayService;
    private Essay currentEssay;


    @RequestMapping("/index")
    public String goIndex(HttpSession session) {
        List<Essay> essays = essayService.findAllEssay();
        session.setAttribute("essay",essays);
        return "myIndex";
    }

    //查询所有的推文
    /*管理员界面查看所有推文*/
    @RequestMapping("/findAllEssays")
    public String findAllEssays(HttpServletRequest request) {
        List<Essay> essays = essayService.findAllEssay();
        request.setAttribute("essay",essays);
        return "icanessay";
    }

    /*普通用户查看所有推文*/
    @RequestMapping("/findAllEssay")
    public String findAll(HttpServletRequest request) {
        List<Essay> essays = essayService.findAllEssay();
        request.setAttribute("essay",essays);
        return "essayShow";
    }

    @RequestMapping("/findEachEssay")
    public String findEachEssay(HttpServletRequest request,int id){
        Essay essay =essayService.findEachEssay(id);
        request.setAttribute("essay",essay);
        return "essay_each";
    }
    @RequestMapping("/checkEssayByName")
    public String checkEssayByName(HttpServletRequest request,String title){
        Essay essay =essayService.checkEssayByName(title);
        request.setAttribute("essay",essay);
        return "editessay";
    }

    /*@RequestMapping("/updateEssay")
    public ModelAndView updateEssay(int id,String title, String author, String content,
                                    String type,Date time) throws IOException {
        ModelAndView mv = new ModelAndView("editessay");
        int result;
            result = essayService.updateEssay(this.currentEssay.getEssay_Id(),title,author,content,time,type);

        if(result != 0){
            this.currentEssay = essayService.findEssayById(this.currentEssay.getEssay_Id());
            System.out.println("推文更新成功："+this.currentEssay.getEssay_title()+"=="+this.currentEssay.getEssay_author()+"=="+this.currentEssay.getEssay_content());
            mv.addObject("msg","操作成功！");
            return mv;
        }else{
            mv.addObject("msg","操作失败！");
            return mv;
        }
    }*/

    @RequestMapping("/findEssayById")
    public String findEssayById(HttpServletRequest request,int id){
        Essay essay =essayService.findEssayById(id);
        request.setAttribute("essay",essay);
        return "essayDet";
    }

    @RequestMapping("/insertEssay")
    public void insertEssay(HttpServletRequest request,HttpServletResponse response,HttpSession session,
                            String title,String author,String content,String type) throws IOException {
        System.out.print("别乱码了，球球了");
        //request.getContentType();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        Date creatDate = new Date();
        java.sql.Date time = new java.sql.Date(creatDate.getTime());
        /*先检查是否有同名数据*/
        Essay tempEssay = essayService.checkEssayByName(title);
        /*不为null说明已经有数据，返回同名发布页面并提示错误信息*/
        if(tempEssay != null){
            /*在页面中以提示框形式输出提示信息*/
            response.getWriter().print("<script type='text/javascript'>alert('相同标题文章已存在！');window.history.go(-1)</script>");
            return;
        }
        int result = essayService.insertEssay(title,author,content,time,type);
        /*测试用*/
        System.out.println(title+"--"+author+"--"+content+"--"+type+"--"+result);
        if(result != 0){
            Essay currentEssay = essayService.checkEssay(title, author, content, time, type);
            /*将对象设置到session中*/
            session.setAttribute("currentEssay", currentEssay);
            response.getWriter().print("<script type='text/javascript'>alert('推文发布成功！')</script>");
            /*成功，转到allessay页面*/
            response.sendRedirect("icanessay");
            System.out.println("发布成功！请查看！");
        }else{
            response.getWriter().print("<script type='text/javascript'>alert('推文发布失败！');window.history.go(-1)</script>");
        }
    }

/*    @RequestMapping("/findEssayTitle")
    public String findEssayTitle(HttpServletRequest request,int id){
        String title = essayService.findEssayTitle(id);
        request.setAttribute("title",title);
        return "title";
    }*/

    @RequestMapping("/updateEssay")
    public void updateEssay(HttpServletRequest request,HttpServletResponse response,HttpSession session,
                            int id, String title,String author,String content,String type) throws IOException{
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("UTF-8");
        Essay essay =essayService.checkEssayByName(title);
        request.setAttribute("essay",essay);
        Date creatDate = new Date();
        java.sql.Date time = new java.sql.Date(creatDate.getTime());
        int result = essayService.updateEssay(id,title,author,content,time,type);
        /*测试用*/
        System.out.println(title+"--"+author+"--"+content+"--"+type+"--"+result);
        if(result != 0){
            Essay currentEssay = essayService.checkEssay(title, author, content, time, type);
            /*将对象设置到session中*/
            session.setAttribute("currentEssay", currentEssay);
            response.getWriter().print("<script type='text/javascript'>alert('推文修改成功！')</script>");
            /*成功，转到icanessay页面*/
            //response.sendRedirect("icanessay");
            System.out.println("修改成功！请查看！");
        }else{
            response.getWriter().print("<script type='text/javascript'>alert('推文修改失败！');window.history.go(-1)</script>");
        }
        //response.sendRedirect("editessay");
    }
    @RequestMapping("/deleteEssay")
    public void deleteEssay(HttpServletRequest request,HttpServletResponse response,HttpSession session,
                            int id) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        boolean isDelete = essayService.deleteEssay(this.currentEssay.getEssay_Id());
        if(isDelete){
            /*清除session*/
            session.removeAttribute("currentEssay");
            this.currentEssay = null;
            response.getWriter().print("<script type='text/javascript'>alert('删除成功！')");
            //response.sendRedirect("index");
        }else {
            response.getWriter().print("<script type='text/javascript'>alert('删除失败！');window.history.go(-1)");
        }
    }
}
